<template>
    <div class="">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-message"></i> 展会轮播图列表</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <el-button type="primary" round @click="handleEdit(0,0)"><i class="el-icon-plus"> </i>&nbsp;&nbsp;新增展会轮播图</el-button>
            <div style="height:40px"></div>
                <!-- 查询条件 -->
                <el-form :inline="true"  class="demo-form-inline">
                    <el-form-item label="轮播图类型:">
                        <el-select v-model="type" placeholder="轮播图类型"  @change="onSelect">
                            <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
					   </el-select>
                    </el-form-item>
                    <el-form-item>
                        <!-- <el-button type="primary" @click="onSelect"> &nbsp;&nbsp;查询&nbsp;&nbsp;  </el-button> -->
                    </el-form-item>
                </el-form>
                <el-table :data="tableData"  style="width: 100%"   ref="multipleTable">
                    <el-table-column prop="id" label="ID" width="120" align="center">
                    </el-table-column>
                    
                     <el-table-column label="图片" width="500" align="center">
                        <template slot-scope="scope" >
                            <div><img :src="scope.row.img" style="width:100%"></div>
                        </template>
                    </el-table-column>
                   <el-table-column prop="trans_url_type" label="跳转类型" width="300" align="center">
                    </el-table-column>
                    <el-table-column prop="trans_type" label="轮播图位置" width="150" align="center">
                    </el-table-column>
                    <el-table-column label="操作" align="center" >
                        <template slot-scope="scope" >
                            <el-button class="edit" size="small" type="text" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                            <el-button class="del" size="small" type="text"  @click="handleDelete(scope.$index, scope.row)" >删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination" v-show="totalPages>1">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next" :page-size="pageSize" :total="totalElements" background>
                    </el-pagination>
                </div>
                
        </div>
        <!-- 新增编辑弹出框 -->
        <el-dialog :title="title" :visible.sync="isAddAccout"  center >
            <el-form style="width:680px; margin:0 auto"  label-width="110px">
                <el-form-item label="图片：">
                    <div class="sola-upload clearfix">
                        <div class="sola-left clearfix" v-show="ePic!=''">
                            <div  class="sola-flex" >
                                <img  :src="ePic" />
                                <span class="delete" @click="delPicture()"><i class="el-alert__icon el-icon-error"></i></span>
                            </div>
                        </div>
                        <div class="sola-file-box" >
                            <el-button type="primary" plain>上传图片<i class="el-icon-upload el-icon--right"></i></el-button>
                            <input class="btn-file" type="file" @change="uploadBanner" v-loading.fullscreen.lock="fullscreenLoading"/>
                            <span style="color:red;padding-left:10px;">建议上传的轮播图比例是71：24   示例：710*240</span>
                        </div>
                    </div>
                </el-form-item>
                <el-form-item label="轮播图类型:">
                        <el-select v-model="ptype" placeholder="请选择" @change="changePosition"> 
                            <el-option v-for="item in typeOtions"  :key="item.value" :label="item.label" :value="item.value">
                            </el-option>
                        </el-select>
                </el-form-item> 
                <el-form-item label="跳转目标类型:">
                        <el-select v-model="url" placeholder="请选择" @change="changeType"> 
                            <el-option v-for="item in urlOptions" :key="item.url" :label="item.label" :value="item.url">
                            </el-option>
                        </el-select>
                </el-form-item> 
                <el-form-item label="跳转目标ID："  v-show="url!=0">
                    <el-input style="width:300px;" v-model="eId" auto-complete="off"  placeholder="请输入跳转目标ID" ></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="onCancel">取 消</el-button>
                <el-button type="primary" @click="onCommit">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 删除图片提示框 -->
        <el-dialog title="提示" :visible.sync="delVisibleImg" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisibleImg = false">取 消</el-button>
                <el-button type="primary" @click="deleteImg">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除图片提示框 -->
        <!-- 删除提示框 -->
        <el-dialog title="提示" :visible.sync="delVisible" width="300px" center>
            <div class="del-dialog-cnt">删除不可恢复，是否确定删除？</div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="delVisible = false">取 消</el-button>
                <el-button type="primary" @click="deleteRow">确 定</el-button>
            </span>
        </el-dialog>
        <!-- / 删除提示框 -->
    </div>
</template>
<script>
import {postUploadBase64} from '@/api/public'
import {getBannerList,postDeleteBanner,postAddBanner,postEditBanner} from '@/api/index/banner'
export default {
    name: "recruitList",
    data() {
        return {
            options: [{
			          value: 9,
			          label: '展会顶部'
			        }, {
			          value: 11,
			          label: '展会光与影'
			        }, {
			          value: 12,
			          label: '展会专访'
                    },{
			          value: 13,
			          label: '国内展会'
                    },{
			          value: 14,
			          label: '国际展会'
                    },
            ],
            type:9,//轮播图类型：1首页、2学院、3协会、4媒体、5印厂、6分站首页、7最新资讯、8推荐资讯、9展会1，
                     //10展会2，11展会光与影、12展会专访、13国内展会、14国际专访
            cityId:0,//分站ID，只有分站首页轮播图生效，其他类型该字段无效
            accept: 'image/gif, image/jpeg, image/png, image/jpg', //图片上传类型
            pageIndex:1,//当前页码
            pageSize:10,//每页10条
            totalElements:1,//默认的总条数
            totalPages:1,//默认的总页数
            tableData:[],//表格数据
            delIndex:0, //当前选中的下标
            delId:0,  //当前要删除的Id
            fullscreenLoading:false,
            delVisible:false, //默认dialog 不弹出
            title:'新增轮播图',//弹出界面标题，默认新增
            delVisibleImg:false,//删除图片
            isAddAccout:false,//弹出框显示隐藏
            dialogState:false,//弹出框的状态 false代表新增 true是修改
            urlOptions:[
                {url:0,label:"不跳转"},{url:2,label:"展会"},
            ],//跳转目标类型：0不跳转，1资讯，2展会，3印厂，4设备，5招聘，6媒体，7协会，8学院，9视频
            url:'',//urlType的取值 跳转类型
            id:'',//当前对象id
            eId:'',//跳转目标id
            ePic:'',//编辑或新增banenr图片地址
            typeOtions: [{
			          value: 9,
			          label: '展会顶部'
			        }, {
			          value: 11,
			          label: '展会光与影'
			        }, {
			          value: 12,
			          label: '展会专访'
                    },{
			          value: 13,
			          label: '国内展会'
                    },{
			          value: 14,
			          label: '国际展会'
                    },
            ],
            ptype:'',//轮播图类型：1首页、2学院、3协会、4媒体、5印厂、6分站首页、7最新资讯、8推荐资讯、9展会1，
                     //10展会2，11展会光与影、12展会专访、13国内展会、14国际专访
        }
    },
    //实例创建
    created(){
        //
        this.getDate();
    },
    //计算属性
    computed:{

    },
    //方法
    methods: {
        //获取数据
        getDate(){
            getBannerList(this.type,0,this.pageIndex,this.pageSize).then((response)=> {
                // console.log(response);
                if(response.code==200){
                    var data = response.data;//当前返回的数据
                    this.tableData = data.list;//列表数据
                    this.totalElements = data.total; //总条数
                    this.totalPages = Math.ceil(data.total/this.pageSize);//总页数
                }else{
                    this.$notify.error({
                        title: '异常',
                        offset: 100,
                        message: response.msg
                    });
                }
            })
        },
        //分页
        handleCurrentChange(val){
            this.pageIndex =val;
            this.getDate();
        },
         //类型change事件
        changeType(){
            this.eId = '';//清空跳转id输入框
        },
        //编辑或新增 显示弹出界面
        handleEdit(index,row){
            this.ptype = '';
            // console.log(row);
            if(row.id && row.id>0){//编辑状态
                this.id = row.id;
                this.title = '修改轮播图'
                this.ePic = row.img;
                this.ptype = row.type;
                this.url = row.url_type;
                this.eId = row.url_value;
                this.dialogState = true;
            }else{//新增状态
                this.title = '新增轮播图'
                //每次新增清空原有数据 
                this.ePic = '';
                this.eId = '';
                this.url = '';
                this.dialogState = false;
            }
            this.isAddAccout = true;
        },
        //提示删除列表
        handleDelete(index,row){
            // console.log(index,row.id);
            if(row.id && row.id>0){
                this.delIndex =index;//要删除的下标
                this.delId = row.id; //要删除的ID
                this.delVisible = true; //显示兑换弹出框
                 //console.log(this.delVisible);
            }
        },
        // 确定删除列表
        deleteRow(){
            //console.log(this.delId)
            if(this.delId>0){
                console.log(this.delId)
                postDeleteBanner(this.delId).then(response=>{
                    console.log(response);
                    if(response.code==200){
                        this.tableData.splice(this.delIndex, 1);
                        this.$message.success('删除成功');
                        this.delVisible = false;
                    }else{
                        this.$notify.error({
                            title: '删除失败',
                            duration:2000,
                            message: '失败原因：'+response.data.msg
                        });
                    }
                    
                })
            }
            
        },
        //取消新增或编辑操作
        onCancel(){
            this.isAddAccout = false;
        },
        //确定新增或编辑
        onCommit(){
           
            if(this.ePic==""){
                this.$notify.info({title: '提示',message: '请先上传图片'});
                return false;
            }
            if(this.ptype==""){
                this.$notify.info({title: '提示',message: '请先选择轮播图类型'});
                return false;
            }
            if(this.url === ""){//全相等  跳出 0 和 空 相等的坑
                
                this.$notify.info({title: '提示',message: '请先选择跳转目标类型'});
                return false;
            }
            else{
               
                let param ={}
                param.type = this.ptype;//新增轮播图类型  9展会1，10展会2，11展会光与影、12展会专访、13国内展会、14国际专访

                param.cityId = this.cityId;
                param.img=this.ePic;
                param.urlType=this.url;
               

                if(this.url == ''){//包含0和空
                    param.urlValue=0 //不跳转传0  
                }else{
                    if(this.eId == ''){
                        this.$notify.info({title: '提示',message: '请先填写跳转目标详情ID'});
                        return false;
                    }else{
                        param.urlValue=this.eId;
                    }
                  
                }

                if(this.dialogState){//编辑
                    param.id = this.id;
                    this.subEdit(param);  
                }else{//新增
                    this.subAdd(param);  
                }
            }    
        },
        //提交新增
        subAdd(param){
            let _this = this ;
            postAddBanner(param).then(response=>{
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，新增成功',
                        type: 'success'
                    });
                    this.isAddAccout =false;
                    setTimeout(function(){
                        _this.getDate();
                    },1500);

                }else{
                    this.$notify.error({
                        title: '新增失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }
            }).catch(error=>{
                console.log(error);
            });
        },
        //提交编辑
        subEdit(param){
            let _this = this ;
            postEditBanner(param).then(response=>{
                if(response.code==200){
                    this.$message({
                        message: '恭喜你，修改成功',
                        type: 'success'
                    });
                    this.isAddAccout =false;
                    setTimeout(function(){
                        _this.getDate();
                    },1500);

                }else{
                    this.$notify.error({
                        title: '修改失败',
                        duration:2000,
                        message: '失败原因：'+response.msg
                    });
                }
            }).catch(error=>{
                console.log(error);
            });
        },
        //显示删除图片提示
        delPicture(){
            //当前记录的下标
            this.delVisibleImg = true;
        },
        //确定删除图片
        deleteImg(){
            this.ePic="";
            this.$message.success('删除成功');
            this.delVisibleImg = false;
        },
        //触发项目封面图 file
        uploadBanner(event){
            var _event = event;
            var _this = this;
            let reader =new FileReader();  
            let img1=event.target.files[0];  
            let type=img1.type;//文件的类型，判断是否是图片  
            let size=img1.size;//文件的大小，判断图片的大小
            const files = event.target;  
            if(this.accept.indexOf(type) == -1){  
                this.$message({
                    message: '请选择gif,jpeg,png,jpg格式图片上传',
                    type: 'warning'
                });
                return false;  
            }  
            else if(size>3145728){  
                this.$message({
                    message: '请选择3M以内的图片上传',
                    type: 'warning'
                }); 
                return false;  
            }else if (typeof(FileReader) === 'undefined') {
                this.$notify.error({
                    title: '错误',
                    message: '抱歉，你的浏览器不支持 FileReader，请使用现代浏览器操作！'
                });

            }else {
                this.fullscreenLoading = true;
                let reader = new FileReader();
                reader.readAsDataURL(img1);
                reader.onload = function(e) {
                    var base = this.result;
                    // console.log(base);
                    
                    postUploadBase64(base).then(response=>{
                        // console.log(response);
                        if(response.code==200){
                            _this.$message({
                                message: '恭喜你，图片上传成功',
                                type: 'success'
                            });
                            _this.ePic=response.data;
                            
                        }else{
                            _this.$notify.error({
                                title: '上传图片失败',
                                duration:2000,
                                message: '失败原因：'+response.msg
                            });
                        }
                        _this.fullscreenLoading = false;
                    })

                }
                
            }
        },
        //查询
        onSelect(){
            this.getDate();
        },
        //轮播图位置类型
        changePosition(){
            console.log(this.ptype);
        },
    }
}
</script>
<style lang="stylus" scoped>
    .edit{
        color #ff9800;
    }
    .del{
        color #e51c23;
    }
    .download{
        color #67C23A;
    }
    .sola-upload{
        .sola-left{
            width :100%;
            height :auto;
            .sola-flex{
                float:left
                width: 631px;
                height: 213px;
                margin-bottom 20px;
                margin-right:15px;
                position:relative;
                .delete{
                    position:absolute;
                    top:2px;
                    right:13px;
                    color: #f56c6c;
                    display:inline-block;
                    padding:5px 5px;
                    .el-alert__icon{font-size:28px;cursor:pointer}
                }
                img {
                    width:100%;
                    height:100%;
                }
            }
        }
        .sola-file-box{
            position:relative;
            .btn-file{
                position:absolute;
                top:0;
                left:0;
                z-index 1;
                opacity:0;
                width :120px;
                height :40px;
                outline: 0;
                background-color :none;
                cursor:pointer;
            }
        }

    }
</style>


